package com.wst.system.util;

import com.alibaba.fastjson2.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

public class TextEncryptUtils {

    public static SecretKey generateAesKey() {
        try {
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128); // 生成128位的AES密钥
            return keyGen.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String keyToString(SecretKey key) {
        byte[] keyBytes = key.getEncoded();
        return Base64.getEncoder().encodeToString(keyBytes);
    }

    public static SecretKey stringToKey(String keyStr) {
        byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        return new SecretKeySpec(keyBytes, 0, keyBytes.length, "AES");
    }

    public static String encryptText(String text, SecretKey key) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decryptText(String encryptedText, SecretKey key) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            return new String(decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encryptText(JSONObject jsonObject,SecretKey publicKey,String privateKey){
        //私钥
        SecretKey secretKeyPrivate =stringToKey(privateKey);
        //加密
        String encryptedText = encryptText(jsonObject.toString(), publicKey);
        //私钥加密
        return encryptText(encryptedText, secretKeyPrivate);
    }

    public static JSONObject decryptText(String encryptedText, String publicKey,String privateKey){
        //私钥
        SecretKey secretKeyPrivate =stringToKey(privateKey);
        //公钥
        SecretKey secretKeyPublic =stringToKey(publicKey);
        //私钥解密
        String decryptedText = decryptText(encryptedText, secretKeyPrivate);
        //公钥解密
        return JSONObject.parseObject(decryptText(decryptedText,secretKeyPublic));
    }

    public static void main(String[] args) {
        String key_1="NXxgoRNXV/5vwjOq4XgFZA==";
        String s2="2719685301056077";
        String s1="OcfDapp6ax2nIEhqmKlwspFAvvBqj6nRC3kPnh6zmlqi+X98Z3rH7uYvCo6/u5tEnb8VxcjkLgr+6iw3bIqzyHioikGtb7Gv5SRIwMyg7m5DppJkQb9/ACKCpPdkAyHDeVWw9t8RcJyunmDtdNV0LlwHqqxh5eF4m6kbIkrdbiyWytZhbEqEmaxgp3e9RYDu";
        SecretKey privateKey =stringToKey(key_1);

        SecretKey aesKey = generateAesKey();
        String key= keyToString(aesKey);

        SecretKey aesKey_1 =stringToKey(key);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",1);
        jsonObject.put("fileId",1);
        jsonObject.put("time",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        String encryptedText = encryptText(jsonObject.toString(), aesKey_1);
        encryptedText=encryptText(encryptedText, privateKey);

        System.out.println("Encrypted Text: " + encryptedText);
        String decryptedText=decryptText(encryptedText, privateKey);
        decryptedText = decryptText(decryptedText, aesKey_1);

        System.out.println("Decrypted Text: " + decryptedText);
    }
}
